package com.gxuwz.controller;

import com.gxuwz.entity.Classes;
import com.gxuwz.entity.Course;
import com.gxuwz.entity.Leave;
import com.gxuwz.entity.dto.LeaveDto;
import com.gxuwz.entity.vo.LeaveVo;
import com.gxuwz.entity.vo.PageVo;
import com.gxuwz.result.Code;
import com.gxuwz.result.PageInfo;
import com.gxuwz.result.ResultInfo;
import com.gxuwz.service.ClassesService;
import com.gxuwz.service.CourseService;
import com.gxuwz.service.LeaveService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @Description:请假申请管理模块
 */

@Controller
@RequestMapping("/leaveApply")
public class LeaveApplyController {

    @Autowired
    private LeaveService leaveService;

    @Autowired
    private ClassesService classesService;

    @Autowired
    private CourseService courseService;

    /**
     * @Description:查询请假申请信息，并跳转至辅导员申请记录管理页面
     * @Param:  [com.gxuwz.entity.dto.LeaveDto, com.gxuwz.result.PageInfo, org.springframework.web.servlet.ModelAndView, javax.servlet.http.HttpServletRequest]
     * @Return: org.springframework.web.servlet.ModelAndView
     */
    
    @RequestMapping("/approveLeaveList")
    public ModelAndView getLeaveApproveList(LeaveDto leaveDto, PageInfo pageInfo, ModelAndView modelAndView, HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        leaveDto.setInstID(username);
        PageVo approveLeaveList = leaveService.getApproveLeaveList(leaveDto, pageInfo);
        List<Classes> classesList = classesService.queryClassesIdAndClassName(username);
        List<String> years = courseService.queryCourseYear();
        modelAndView.addObject("classes",classesList);
        modelAndView.addObject("years",years);
        modelAndView.setViewName("leaveApplyManage/leaveApprove");
        modelAndView.addObject("pageVo",approveLeaveList);
        return modelAndView;
    }

    /**
     * @Description:实现辅导员批准学生请假申请功能，并跳转页面至请假申请管理页面
     * @Param:  [org.springframework.web.servlet.ModelAndView, java.lang.String]
     * @Return: org.springframework.web.servlet.ModelAndView
     */
    
    @GetMapping("/approveLeaveInfo")
    public ModelAndView getApproveLeaveInfo(ModelAndView modelAndView,String leaveID){
        LeaveVo leaveVo = leaveService.getApproveLeaveInfo(leaveID);
        modelAndView.setViewName("leaveApplyManage/approveManage");
        modelAndView.addObject("leave",leaveVo);
        return modelAndView;
    }

    /**
     * @Description:实现查询相应请假申请信息，并实现辅导员批准请假申请信息和跳转至请假申请管理页面功能
     * @Param:  [org.springframework.web.servlet.ModelAndView, java.lang.String, java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @RequestMapping("/updateLeaveInfo")
    public ModelAndView updateLeaveInfo(ModelAndView modelAndView,String leaveID,String opinion,String leaveStatus,HttpServletRequest request){
        boolean flag = leaveService.approveLeaveInfo(leaveID, opinion, leaveStatus);
        PageInfo pageInfo = new PageInfo();
        LeaveDto leaveDto = new LeaveDto();
        HttpSession session = request.getSession();
        leaveDto.setInstID((String) session.getAttribute("username"));
        if(flag){
            modelAndView.addObject("resultInfo",new ResultInfo(Code.DELETE_OK,"审批成功"));
        } else {
            modelAndView.addObject("resultInfo",new ResultInfo(Code.DELETE_ERR,"审批失败"));
        }
        PageVo pageVo = leaveService.getApproveLeaveList(leaveDto, pageInfo);
        modelAndView.setViewName("leaveApplyManage/leaveApprove");
        modelAndView.addObject("pageVo",pageVo);
        return modelAndView;
    }

    /**
     * @Description:实现学生端查询自己的请假申请功能，并跳转至学生请假申请信息管理页面
     * @Param:  [com.gxuwz.result.PageInfo, org.springframework.web.servlet.ModelAndView, com.gxuwz.entity.dto.LeaveDto]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @RequestMapping("/list")
    public ModelAndView getLeaveApplyList(PageInfo pageInfo, ModelAndView modelAndView, LeaveDto leaveDto,HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        PageVo pageVo = leaveService.getLeaveList(leaveDto,pageInfo,username);
        modelAndView.addObject("pageVo",pageVo);
        modelAndView.setViewName("leaveApplyManage/leaveApplyManage");
        return modelAndView;
    }

    /**
     * @Description:跳转至添加请假申请记录页面
     * @Param:  [org.springframework.web.servlet.ModelAndView]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @GetMapping("/addLeavePage")
    public ModelAndView queryIds(ModelAndView modelAndView,HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        String leaveID = leaveService.getLeaveID();
        System.out.println(leaveID);
        List<Course> courseList = courseService.queryCourseIdAndCourseName(username);
        modelAndView.addObject("courses",courseList);
        modelAndView.addObject("leaveID",leaveID);
        modelAndView.setViewName("leaveApplyManage/addLeaveApply");
        return modelAndView;
    }

    /**
     * @Description:实现添加请假申请记录功能，并跳转至请假申请信息管理页面
     * @Param:  [org.springframework.web.servlet.ModelAndView, com.gxuwz.entity.Leave]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @RequestMapping("/addLeaveApply")
    public ModelAndView addLeaveApply(ModelAndView modelAndView,Leave leave,HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        System.out.println(leave.toString());
        boolean flag = leaveService.addLeaveInfo(leave);
        PageInfo pageInfo = new PageInfo();
        if(flag){
            modelAndView.addObject("resultInfo",new ResultInfo(Code.ADD_OK,"新增成功"));
        } else {
            modelAndView.addObject("resultInfo",new ResultInfo(Code.ADD_ERR,"新增失败"));
        }
        PageVo pageVo = leaveService.getLeaveList(null, pageInfo,username);
        modelAndView.setViewName("leaveApplyManage/leaveApplyManage");
        modelAndView.addObject("pageVo",pageVo);
        return modelAndView;
    }

    /**
     * @Description:查询相应请假申请信息，并跳转至查看申请详细信息页面
     * @Param:  [java.lang.String, org.springframework.web.servlet.ModelAndView]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @GetMapping("/checkLeaveApplyPage")
    public ModelAndView checkLeaveApplyPage(String leaveID,ModelAndView modelAndView){
        System.out.println(leaveID);
        LeaveVo leaveVo = leaveService.getLeaveOneByLeaveID(leaveID);
        modelAndView.addObject("leaveVo",leaveVo);
        modelAndView.setViewName("leaveApplyManage/checkLeaveInfo");
        return modelAndView;
    }

    /**
     * @Description:实现删除申请信息功能，并跳转至申请信息管理页面
     * @Param:  [java.lang.String, org.springframework.web.servlet.ModelAndView]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @RequestMapping("/deleteleaveApply")
    public ModelAndView deleteLeaveApply(String leaveId, ModelAndView modelAndView,HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        boolean flag = leaveService.deleteLeaveOne(leaveId);
        PageInfo pageInfo = new PageInfo();
        if(flag){
            modelAndView.addObject("resultInfo",new ResultInfo(Code.DELETE_OK,"删除成功"));
        } else {
            modelAndView.addObject("resultInfo",new ResultInfo(Code.DELETE_ERR,"删除失败"));
        }
        PageVo pageVo = leaveService.getLeaveList(null, pageInfo,username);
        modelAndView.setViewName("leaveApplyManage/leaveApplyManage");
        modelAndView.addObject("pageVo",pageVo);
        return modelAndView;
    }

    /**
     * @Description:实现批准请假申请功能，并跳转至请假申请信息管理页面
     * @Param:  [org.springframework.web.servlet.ModelAndView, java.lang.String]
     * @Return: org.springframework.web.servlet.ModelAndView
     */
    
    @GetMapping("/approveInfo")
    public ModelAndView getApproveInfo(ModelAndView modelAndView,String leaveID){
        LeaveVo leaveVo = leaveService.getApproveLeaveInfo(leaveID);
        modelAndView.setViewName("leaveApplyManage/approveLeaveInfo");
        modelAndView.addObject("leave",leaveVo);
        return modelAndView;
    }

    /**
     * @Description:实现导出请假申请记录功能
     * @Param:  [javax.servlet.http.HttpServletResponse, javax.servlet.http.HttpServletRequest, com.gxuwz.entity.dto.LeaveDto]
     * @Return: void
     */
    
    @RequestMapping("/exportLeaveAll")
    public void exportLeaveAll(HttpServletResponse response,HttpServletRequest request,LeaveDto leaveDto){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        leaveDto.setInstID(username);
        List<LeaveVo> exportLeaveList = leaveService.getExportLeaveList(leaveDto);
        //创建HSSWorkbook对象，对应一个excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建一个HSSFSheet对象，对应wb文件中的一页
        HSSFSheet sheet = wb.createSheet("请假单");//设置页名
        //创建HSSFRow对象,对应sheet中的一行
        HSSFRow row = sheet.createRow(0);//索引0表示第一行
        //创建HSSFCell对象，对应sheet中的一列
        HSSFCell cell = row.createCell(0);//索引0表示第一列
        //设置列名
        cell.setCellValue("请假单号");
        cell = row.createCell(1);
        cell.setCellValue("学生编号");
        cell = row.createCell(2);
        cell.setCellValue("学生姓名");
        cell = row.createCell(3);
        cell.setCellValue("课程名称");
        cell = row.createCell(4);
        cell.setCellValue("申请时间");
        cell = row.createCell(5);
        cell.setCellValue("请假天数");
        cell = row.createCell(6);
        cell.setCellValue("请假理由");
        cell = row.createCell(7);
        cell.setCellValue("审批情况");
        cell = row.createCell(8);
        cell.setCellValue("审批时间");
        cell = row.createCell(9);
        cell.setCellValue("审批意见");

        if(exportLeaveList!=null&&exportLeaveList.size()>0){
            LeaveVo leaveVo = null;
            for (int i = 0; i < exportLeaveList.size(); i++){
                leaveVo = exportLeaveList.get(i);
                row = sheet.createRow(i+1);
                cell = row.createCell(0);
                cell.setCellValue(leaveVo.getLeaveID());
                cell = row.createCell(1);
                cell.setCellValue(leaveVo.getStuID());
                cell = row.createCell(2);
                cell.setCellValue(leaveVo.getStuName());
                cell = row.createCell(3);
                cell.setCellValue(leaveVo.getCourseName());
                cell = row.createCell(4);
                cell.setCellValue(leaveVo.getApplyTime());
                cell = row.createCell(5);
                cell.setCellValue(leaveVo.getDayNum());
                cell = row.createCell(6);
                cell.setCellValue(leaveVo.getReason());
                cell = row.createCell(7);
                cell.setCellValue(leaveVo.getStatus());
                cell = row.createCell(8);
                cell.setCellValue(leaveVo.getAuditTime());
                cell = row.createCell(9);
                cell.setCellValue(leaveVo.getOpinion());
            }
        }

        //设置响应类型application/octet-stream 表示excel文件
        response.setContentType("application/octet-stream;charset=UTF-8");
        //设置响应头 Content-Disposition打卡方式 attachment 以附件的形式
        response.addHeader("Content-Disposition","attachment;filename=AllactivityList.xls");
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            wb.write(outputStream);
            wb.close();
            outputStream.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
